<template>
    <div class="wrapper wrapper-content animated fadeInRight">

        <div class="row">
            <div class="col-sm-12">
                <div class="ibox float-e-margins">
                    <div class="ibox-title">
                        <h5>主机厂编辑</h5>

                    </div>
                    <div class="ibox-content">
                        <form method="get" class="form-horizontal">
                            <div class="form-group">
                                <label class="col-sm-2 control-label">品牌</label>
                                <div class="col-sm-10">
                                    <select class="form-control m-b" v-model="factory.brand_id">
                                        <option v-for="brand in brands" v-bind:value="brand.id" >
                                            {{brand.name}}
                                        </option>
                                    </select>
                                </div>
                            </div>
                            <div class="hr-line-dashed"></div>

                            <div class="form-group">
                                <label class="col-sm-2 control-label">名称</label>

                                <div class="col-sm-10">
                                    <input type="text" v-model='factory.name' class="form-control">
                                </div>
                            </div>
                            <div class="hr-line-dashed"></div>


                            <div class="form-group">
                                <div class="col-sm-4 col-sm-offset-2">
                                    <a class="btn btn-primary" v-on:click="factory_edit()" >保存内容</a>
                                    <!--<button class="btn btn-white" type="submit">取消</button>-->
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
    import config from '../config'
    export default{
        name : 'factory_edit',
        factory_id : 0,
//        brand:{
//            'name':'',
//            'letter_pre':''
//        },
        data () {
            return {
                factory:{
                    'name':'',
                    'brand_id':''
                },
                brands:[],
                getFactory : function(){
                    var api_token = localStorage.getItem('api_token');
                    var url =  config.api_url + '/backend/factory/' + this.factory_id + '?api_token=' + api_token;
                    var t = this;
                    $.ajax({
                        type: "get",
                        url: url,
                        dataType: 'json',
                        data: {},
                        xhrFields:true,
//                        xhrFields:"'Access-Control-Allow-Origin': '*'",
                        success:function( json ){
                            if( json.status == undefined || json.status == 0 ) {
                                t.factory = json;
                            }else{
                                alert( json.description );
                                if( json.status == 2 ){
                                    location.href = '/login.html';
                                }
                            }
                        }
                    });

                }
                ,factory_edit : function(){
                    var api_token = localStorage.getItem('api_token');
                    var url =  config.api_url + '/backend/factory/' + this.factory_id + '/edit' + '?api_token=' + api_token;
                    var t = this;
                    $.ajax({
                        type: "post",
                        url: url,
                        dataType: 'json',
                        data: t.factory,
                        xhrFields:true,
                        success:function( json ){

                            if( json.status == undefined || json.status == 0 ) {
                                alert('保存成功');
                                location.href = '#/factory';
                            }else{
                                alert( json.description );
                                if( json.status == 2 ){
                                    location.href = '/login.html';
                                }
                            }
                        }
                    });
                }
                ,getBrands : function(){
                    var api_token = localStorage.getItem('api_token');
                    var url =  config.api_url + '/backend/brands' + '?api_token=' + api_token;
                    var t = this;
                    $.ajax({
                        type: "get",
                        url: url,
                        dataType: 'json',
                        data: {},
                        xhrFields:true,
                        success:function( json ){

                            if( json.status == undefined || json.status == 0 ){
                                for(var i =0;i < json.length; i ++){
                                    t.brands.push( json[i]);
                                }
                            }else{
                                alert( json.description );
                                if( json.status == 2 ){
                                    location.href = '/login.html';
                                }
                            }
                        }
                    });
                }
            }
        }
        ,created: function(){
            this.factory_id = this.$route.params.id;
            this.getBrands();
            this.getFactory();
        }

    }
</script>

